package meijuli.com.join51.business.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import cn.jpush.android.api.JPushInterface
import com.jhx.common.utils.Constants
import meijuli.com.join51.business.R
import com.jhx.common.http.IPServFactory
import com.jhx.common.framework.QuickBaseActivity
import com.jhx.common.http.ApiManager
import meijuli.com.join51.business.model.AppBean
import meijuli.com.join51.business.model.EventBean
import meijuli.com.join51.business.resetpwd.ResetPwdActivity
import com.jhx.common.widget.CommonDialog
import com.jhx.common.utils.ToastUtil
import kotlinx.android.synthetic.main.fragment_setting.*
import meijuli.com.join51.business.service.ApiService
import meijuli.com.join51.business.utils.UpdateCheck
import com.jhx.common.utils.UserManager
import com.jhx.video.activity.VideoSettingActivity
import org.greenrobot.eventbus.EventBus

/**
 * 设置页面
 *
 * @author wenqin 2017-04-14 11:59
 */

class SettingActivity : QuickBaseActivity() {
    private var mUpdateDialog: CommonDialog? = null

    companion object {
        val REQUEST_CALL_PERMISSION = 100
        /**
         * 强制更新
         */
        private val COMPEL_UPDATE = 1
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting)
        setTitle("设置")
        aboutView.setOnClickListener {
            val intent = Intent(this@SettingActivity, AboutActivity::class.java)
            startActivity(intent)
        }

        logoutView.setOnClickListener {
            val dialog = CommonDialog(this@SettingActivity)
            dialog.setMessage("确定退出吗？")
            dialog.setConfirmBtn("退出")
            dialog.setCancelBtn("取消")
            dialog.setOnClickListener(object: CommonDialog.OnClickListener {
                override fun onConfirmClick(view: View?) {
                    loginOut()
                }

                override fun onCancelClick(view: View?) {

                }

            })
            dialog.show()
        }

        change_password.setOnClickListener {
            val intent = Intent(this@SettingActivity, ResetPwdActivity::class.java)
            startActivity(intent)
        }

        check_update.setOnClickListener {
            // 检查更新
            checkUpdate()
        }

        txt_video_setting.setOnClickListener {
            val intent = Intent(this, VideoSettingActivity::class.java)
            startActivity(intent)
        }

        if (Constants.DEBUG) {
            view_stub.visibility = View.VISIBLE
            val ipBtn = findViewById<View>(R.id.ip_view) as TextView
            ipBtn.text = IPServFactory.sharedInstance().serverName
            ipBtn.setOnClickListener {
                // 测试服务器跳转到设置Ip地址
                val intent = Intent(this@SettingActivity, IpActivity::class.java)
                startActivity(intent)
            }
        }

    }

    private fun checkUpdate() {
        UpdateCheck.getUpdateCheck().setUpdateCheckListener(object : UpdateCheck.OnUpdateCheckListener {
            override fun onUpdateCheckStart() {
                showLoadingDialog()
            }

            override fun onUpdateCheckComplete(appBean: AppBean, currentVersion: String) {
                hideLoadingDialog()
                if (UpdateCheck.toVersionCode(currentVersion) < UpdateCheck.toVersionCode(appBean.mainVersion)) {
                    if(mUpdateDialog == null) {
                        mUpdateDialog = CommonDialog(this@SettingActivity)
                    }
                    mUpdateDialog?.setMessage(appBean.appTxt)
                    mUpdateDialog?.setTitle(appBean.appLable)
                    mUpdateDialog?.setConfirmBtn("升级")
                    mUpdateDialog?.setCancelable(false)
                    mUpdateDialog?.setOnClickListener(object: CommonDialog.OnClickListener {
                        override fun onConfirmClick(view: View?) {
                            DownloadActivity.start(this@SettingActivity, appBean)
                            finish()
                        }

                        override fun onCancelClick(view: View?) {
                            if(appBean.flag == COMPEL_UPDATE) {
                                finish()
                                System.exit(0)
                            }
                        }

                    })
                    if (appBean.flag != COMPEL_UPDATE && UpdateCheck.toVersionCode(currentVersion) >= UpdateCheck.toVersionCode(appBean.minVersion)) {
                        /**
                         *
                         * 非强制更新
                         */
                        mUpdateDialog?.setCancelBtn("取消")
                    } else {
                        mUpdateDialog?.setCancelBtn("退出")
                    }
                    if(mUpdateDialog?.isShowing == false) {
                        mUpdateDialog?.show()
                    }
                } else {
                    ToastUtil.getInstance().showToast(this@SettingActivity, "您现在已经是最新版本")
                }
            }

            override fun onUpdateCheckNew() {
                hideLoadingDialog()
                ToastUtil.getInstance().showToast(this@SettingActivity, "已是最新版本")
            }

            override fun onUpdateCheckFail() {
                hideLoadingDialog()
            }
        }).check(true)
    }

    /**
     * 退出登录
     */
    private fun loginOut() {
        ApiManager.get().getApiService(ApiService::class.java).logOut()
        UserManager.get().clear()
        EventBus.getDefault().post(EventBean<Any>(EventBean.STATUS_LOGIN_OUT))
        // 调用 JPush 接口来清除别名。
        JPushInterface.setAliasAndTags(this, "", null, null)
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mUpdateDialog != null) {
            mUpdateDialog?.dismiss()
            mUpdateDialog = null
        }
    }

}
